﻿using Core;

namespace g_gongjianta
{
    public class LifeCycleComponent : Core.Component, IAwake<int>, ILifeCycle
    {
        public bool IsComplete { get; private set; }

        public int Duration { get; private set; }

        private int startFrame;

        public void Awake(int duration)
        {
            Duration = duration;
        }

        public override void Dispose()
        {
            if(IsDisposed) return;
            base.Dispose();
        }

        public void Start()
        {
            startFrame = SysTime.FrameCount;
            IsComplete = false;
        }

        public void Tick()
        {
            var elapsed = SysTime.FrameCount - startFrame;

            if (elapsed >= Duration)
            {
                IsComplete = true;
            }
        }

        public void Finish()
        {
            IsComplete = true;

            Entity.Dispose();
        }

        public void Interrupt()
        {
            IsComplete = true;
        }

    }
}
